// const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { defines, info } = require('./tools/base/defines');
const CopyWebpackPlugin = require('copy-webpack-plugin');
// const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
function toDate(v, format){
    format = format ? format : "yyyy-MM-dd HH:mm"
    if (!v) return "";
    var d = v;
    if (typeof v === 'string') {
      if (v.indexOf("/Date(") > -1)
        d = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
      else
        d = new Date(Date.parse(v.replace(/-/g, "/").replace("T", " ").split(".")[0]));
    } else {
      d = new Date(v)
    }
    var o = {
      "M+": d.getMonth() + 1,
      "d+": d.getDate(),
      "h+": d.getHours(),
      "H+": d.getHours(),
      "m+": d.getMinutes(),
      "s+": d.getSeconds(),
      "q+": Math.floor((d.getMonth() + 3) / 3),
      "S": d.getMilliseconds()
    };
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
      if (new RegExp("(" + k + ")").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
      }
    }
    return format;
  }

const version = toDate(new Date(),'yyyy.MM.dd.HH.mm.ss')
module.exports = {
    outputDir:'scene.'+version +'.beta',
    devServer: {
        client: {
            progress: true,
            overlay: false,
        },
    },
    filenameHashing: false,
    css: {
        extract: true,
    },
    pages: {
        index: {
            // page 的入口
            entry: 'src/main.ts',
            inject: false,
            minify: false,
        },
    },
    devServer: {

        port: 3001,
        proxy: {
            // '/': {
            //     target: 'http://localhost:9004/',
            //     changeOrigin: true, // 允许跨域
            //     pathRewrite: {
            //         '^/': '',
            //     },
            // },
            //sino更新
            '/dev': {
                target: 'http://172.16.20.1:41380',
                // target: 'http://www.sinobroad.com:41224',
                changeOrigin: true, // 允许跨域
                pathRewrite: {
                    '^/dev': ''
                },
            },
        },
        webSocketServer: false
    },
    publicPath: './',
    lintOnSave: false,
    productionSourceMap: false, //打包不生成map
    chainWebpack:config =>{
        config.plugin('html-index').tap(args => {
            console.log(args)
            args[0].version = version
            return args
        })
    },
    configureWebpack: {
        optimization: {
            splitChunks: false,
        },
        externals: {
            // @cesium
            cesium: 'Cesium', // xr-utils中有cesium相关的函数
            // @xbsj-renderer
            'xbsj-renderer/dist-node/xr-base-utils': 'xbsj["xr-base-utils"]',
            'xbsj-renderer/dist-node/xr-math': 'xbsj["xr-math"]',
            'xbsj-renderer/dist-node/xr-utils': 'xbsj["xr-utils"]',
            'xbsj-renderer/dist-node/xr-cesium': 'xbsj["xr-cesium"]',
            // @xbsj-xe2
            'xbsj-xe2/dist-node/xe2': 'XE2["xe2"]',
            'xbsj-xe2/dist-node/xe2-base': 'XE2["xe2-base"]',
            'xbsj-xe2/dist-node/xe2-base-utils': 'XE2["xe2-base-utils"]',
            'xbsj-xe2/dist-node/xe2-utils': 'XE2["xe2-utils"]',
            'xbsj-xe2/dist-node/xe2-cesium': 'XE2["xe2-cesium"]',
            'xbsj-xe2/dist-node/xe2-mapbox': 'XE2["xe2-mapbox"]',
            'xbsj-xe2/dist-node/xe2-ue': 'XE2["xe2-ue"]',
            'xbsj-xe2/dist-node/utility-xe2-plugin': 'XE2["utility-xe2-plugin"]',
            'xbsj-xe2/dist-node/xe2-all-in-one': 'XE2["xe2-all-in-one"]',
            'xbsj-xe2/dist-node/xe2-base-objects': 'XE2["xe2-base-objects"]',
            'xbsj-xe2/dist-node/xe2-objects': 'XE2["xe2-objects"]',
            'xbsj-xe2/dist-node/xe2-cesium-objects': 'XE2["xe2-cesium-objects"]',
            'xbsj-xe2/dist-node/xe2-ue-objects': 'XE2["xe2-ue-objects"]',
            'xbsj-xe2/dist-node/xe2-openlayers': 'XE2["xe2-openlayers"]',
            'xbsj-xe2/dist-node/xe2-openlayers-objects': 'XE2["xe2-openlayers-objects"]',
            'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin': 'XE2["esobjs-xe2-plugin"]',
            'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main': 'XE2["esobjs-xe2-plugin-main"]',
        },
        resolve: {
            alias: {
                "@": path.resolve('./src'),
            }
        },
        plugins: [
            new webpack.BannerPlugin(`${info.name}(${info.version}-${info.commitId.slice(0, 8)}-${info.date}) 版权所有@${info.owner}`),
            new webpack.DefinePlugin(defines),
            // new CleanWebpackPlugin(),
            new CopyWebpackPlugin([
                {
                    from: './node_modules/xbsj-xe2/dist-web',
                    to: 'js/xbsj-xe2/dist-web',
                    toType: 'dir'
                },
                {
                    from: './node_modules/xbsj-xe2-assets/dist-web',
                    to: 'js/xbsj-xe2-assets/dist-web',
                    toType: 'dir'
                },
                {
                    from: './node_modules/esobjs-xe2-plugin/dist-web',
                    to: 'js/esobjs-xe2-plugin/dist-web',
                    toType: 'dir'
                },
                {
                    from: './node_modules/esobjs-xe2-plugin-assets/dist-web',
                    to: 'js/esobjs-xe2-plugin-assets/dist-web',
                    toType: 'dir'
                },
                {
                    from: './node_modules/vue-xe2-plugin/dist-web',
                    to: 'js/vue-xe2-plugin/dist-web',
                    toType: 'dir'
                }
            ]),
        ],
    },
}
